package com.itqf.controller;

import cn.dsna.util.images.ValidateCode;
import com.itqf.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/17
 * @Time: 上午9:33
 */
@Controller
public class UsersController {
    //String  ModelAndView   页面
    //Users  List<Users>  Map   响应json
    @RequestMapping("/doLogin")
    public   String   login(Users users, Model model, HttpSession session){
        //登录业务判断
        if ("admin".equals(users.getUsername())){
            session.setAttribute("users",users);
            return  "welcome.jsp";
        }
        model.addAttribute("errorMsg","用户名或者密码错误");
        return "login.jsp";
    }

    @RequestMapping("/code")
    public  void  code(HttpServletResponse response){
        try {
            ValidateCode code = new ValidateCode(100,30,4,2);
            String text = code.getCode();
            System.out.println(text+"---->");
            ImageIO.write(code.getBuffImg(),"jpg",response.getOutputStream());


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
